<?php
defined('_JEXEC') or die('Restricted access');

class igUploadSwfupload
{
	function swfuploadHeadJs()
	{
		$catid = JRequest::getInt('catid',0);
		$configArray =& JComponentHelper::getParams('com_igallery');
		$flashResize = $configArray->get('client_resize', 0);
		$moderateString = $configArray->get('moderate_img', 0) == 0 || IG_BACKEND == true ? '' : '&moderateMsg=1';
		
		if($flashResize == 1)
		{
		    $basePath = IG_BACKEND == true ? IG_HOST.'administrator/' : IG_HOST;
	
			$document =& JFactory::getDocument();
			$document->addScript(IG_HOST.'administrator/components/com_igallery/lib/uploaders/swfupload/swfupload.js');
			$document->addScript(IG_HOST.'administrator/components/com_igallery/lib/uploaders/swfupload/handlers_rs.js');
			$document->addStyleSheet(IG_HOST.'administrator/components/com_igallery/lib/uploaders/swfupload/default.css');
	
			$session = & JFactory::getSession();
			$Itemid = JRequest::getInt('Itemid', null);
			$ItemIdString = empty($Itemid) ? '' : '&Itemid='.$Itemid;
	
			$configArray =& JComponentHelper::getParams('com_igallery');
			$resizeWidth = $configArray->get('client_resize_width', 2000);
			$resizeHeight = $configArray->get('client_resize_height', 1500);
			$resizeQuality = $configArray->get('client_resize_quality', 100);
	
	        $swfUploadHeadJs ='
			var swfu;
			window.onload = function () {
				swfu = new SWFUpload({
					// Backend Settings
					upload_url: "'.$basePath.'index.php",
					post_params:
					{
						"option" : "com_igallery",
						"controller" : "igphoto",
						"task" : "swfUpload",
						"catid" : "'.$catid.'",
						"'.$session->getName().'" : "'.$session->getId().'",
						"format" : "raw"
					},
	
					// File Upload Settings
					file_size_limit : "15 MB",
					file_types : "*.jpg;*.png",
					file_types_description : "JPG, PNG",
					file_upload_limit : 0,
	
					swfupload_preload_handler : preLoad,
					swfupload_load_failed_handler : loadFailed,
					file_queue_error_handler : fileQueueError,
					file_dialog_complete_handler : fileDialogComplete,
					upload_progress_handler : uploadProgress,
					upload_error_handler : uploadError,
					upload_success_handler : uploadSuccess,
					upload_complete_handler : uploadComplete,
	
					// Button Settings
					button_image_url : "'.IG_HOST.'administrator/components/com_igallery/lib/uploaders/swfupload/browse-background.gif",
					button_placeholder_id : "spanButtonPlaceholder",
					button_width: 120,
					button_height: 29,
					button_text: \'<span class="theFont">'.JText::_( 'CHOOSE FILES' ).'</span>\',
					button_text_style: ".theFont { font-size: 13px; }",
					button_text_left_padding: 21,
					button_text_top_padding: 6,
					button_window_mode: SWFUpload.WINDOW_MODE.TRANSPARENT,
					button_cursor: SWFUpload.CURSOR.HAND,
	
					// Flash Settings
					flash_url : "'.IG_HOST.'administrator/components/com_igallery/lib/uploaders/swfupload/swfupload.swf",
					flash9_url : "'.IG_HOST.'administrator/components/com_igallery/lib/uploaders/swfupload/swfupload_fp9.swf",
	
					custom_settings : {
						upload_target : "divFileProgressContainer",
						thumbnail_width: '.$resizeWidth.',
						thumbnail_height: '.$resizeHeight.',
						thumbnail_quality: '.$resizeQuality.',
						refreshUrl: \'index.php?option=com_igallery&controller=igphoto&view=igphoto&catid='.$catid.$ItemIdString.$moderateString.'\'
					},
	
					// Debug Settings
					debug: false
				});
			};
		    ';
	
			$document->addScriptDeclaration($swfUploadHeadJs);
		}

		else
		{
			$catid = JRequest::getInt('catid',0);
		    $basePath = IG_BACKEND == true ? IG_HOST.'administrator/' : IG_HOST;
	
			$document =& JFactory::getDocument();
			$document->addScript(IG_HOST.'administrator/components/com_igallery/lib/uploaders/swfupload/swfupload.js');
			$document->addScript(IG_HOST.'administrator/components/com_igallery/lib/uploaders/swfupload/swfupload.queue.js');
			$document->addScript(IG_HOST.'administrator/components/com_igallery/lib/uploaders/swfupload/fileprogress.js');
			$document->addScript(IG_HOST.'administrator/components/com_igallery/lib/uploaders/swfupload/handlers.js');
			$document->addStyleSheet(IG_HOST.'administrator/components/com_igallery/lib/uploaders/swfupload/default.css');
	
			$session = & JFactory::getSession();
			$Itemid = JRequest::getInt('Itemid', null);
			$ItemIdString = empty($Itemid) ? '' : '&Itemid='.$Itemid;
	        $swfUploadHeadJs ='
			var swfu;
	
			window.onload = function() {
				var settings = {
				    flash_url : "'.IG_HOST.'administrator/components/com_igallery/lib/uploaders/swfupload/swfupload.swf",
					flash9_url : "'.IG_HOST.'administrator/components/com_igallery/lib/uploaders/swfupload/swfupload_fp9.swf",
					upload_url: "'.$basePath.'index.php",
					post_params:
					{
						"option" : "com_igallery",
						"controller" : "igphoto",
						"task" : "swfUpload",
						"catid" : "'.$catid.'",
						"'.$session->getName().'" : "'.$session->getId().'",
						"format" : "raw"
					},
					file_size_limit : "15 MB",
					file_types : "*.jpg;*.jpeg;*.gif;*.png",
					file_types_description : "All Files",
					file_upload_limit : 300,
					file_queue_limit : 300,
					custom_settings : {
						progressTarget : "fsUploadProgress",
						cancelButtonId : "btnCancel",
						refreshUrl: \'index.php?option=com_igallery&controller=igphoto&view=igphoto&catid='.$catid.$ItemIdString.$moderateString.'\'
					},
					debug: false,
	
					// Button settings
					button_image_url: "'.IG_HOST.'administrator/components/com_igallery/lib/uploaders/swfupload/browse-background.gif",
					button_width: "120",
					button_height: "29",
					button_placeholder_id: "spanButtonPlaceHolder",
					button_text: \'<span class="theFont">'.JText::_( 'CHOOSE FILES' ).'</span>\',
					button_text_style: ".theFont { font-size: 13px; }",
					button_text_left_padding: 21,
					button_text_top_padding: 6,
	
					// The event handler functions are defined in handlers.js
					swfupload_preload_handler : preLoad,
					swfupload_load_failed_handler : loadFailed,
					file_queued_handler : fileQueued,
					file_queue_error_handler : fileQueueError,
					file_dialog_complete_handler : fileDialogComplete,
					upload_start_handler : uploadStart,
					upload_progress_handler : uploadProgress,
					upload_error_handler : uploadError,
					upload_success_handler : uploadSuccess,
					upload_complete_handler : uploadComplete,
					queue_complete_handler : queueComplete	// Queue plugin event
				};
	
				swfu = new SWFUpload(settings);
		     };
		    ';
	
			$document->addScriptDeclaration($swfUploadHeadJs);
		}
	}
	
	function swfuploadHTML()
	{
		$configArray =& JComponentHelper::getParams('com_igallery');
		$flashResize = $configArray->get('client_resize', 0);
		if($flashResize == 0)
		{
		?>
		<div id="swfuploader">
		
		<form id="form1" action="index.php" method="post" enctype="multipart/form-data">
			
			<fieldset class="adminform">
			
				<div class="fieldset flash" id="fsUploadProgress">
					<span class="legend"><?php echo JText::_( 'UPLOAD QUEUE' ); ?></span>
				</div>
				
				<div id="divStatus">
					0 <?php echo JText::_( 'FILES UPLOADED' ); ?>
				</div>
				
				<div>
					<span id="spanButtonPlaceHolder"></span>
					<input id="btnCancel" type="button" value="<?php echo JText::_( 'CANCEL ALL UPLOADS' ); ?>" onclick="swfu.cancelQueue();" disabled="disabled" style="margin-left: 2px; font-size: 13px; height: 29px;" />
					<br />
					<?php echo JText::_( 'HOLD CONTROL SHIFT' ); ?>
				</div>
				
			</fieldset>
			
		</form>
		</div>
		<?php
		}
		else
		{
			?>
			<div id="swfuploader">
			    <form>
			    <fieldset class="adminform">
			    <legend><?php echo JText::_( 'UPLOAD IMAGES' ); ?></legend>
			    	<div style="width: 120px; height: 29px; border: solid 1px #7FAAFF; background-color: #C5D9FF; padding: 2px;">
			    		<span id="spanButtonPlaceholder"></span>
			    	</div><br />
			    	<?php echo JText::_( 'HOLD CONTROL SHIFT' ); ?>
			    </fieldset>
			    </form>
	    
			    <div id="divFileProgressContainer" style="height: 75px;"></div>
			</div>
			<?php
		}
	
	}
	
}

